package com.huike.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.dto.BusinessPageDto;
import com.huike.dto.TransferDto;
import com.huike.entity.*;
import com.huike.mapper.*;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.TransferService;
import com.huike.vo.TransferVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TransferMapper transferMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Override
    public Result queryPage(TransferDto transferDto) {

        if (transferDto.getPage() == null || transferDto.getPageSize() == null) {
            transferDto.setPage(1);
            transferDto.setPageSize(10);
        }
        IPage<Transfer> iPage = new Page<>(transferDto.getPage(), transferDto.getPageSize());

        QueryWrapper<Transfer> queryWrapper = new QueryWrapper<>();
        if (transferDto.getBeginTime() != null && transferDto.getEndTime() != null) {


            LocalDateTime localDateTime = transferDto.getCreateTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();

            LocalDateTime startTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);

            queryWrapper.lambda().between(Transfer::getCreateTime, startTime,endTime);



            queryWrapper.lambda().between(Transfer::getCreateTime, transferDto.getBeginTime(), transferDto.getEndTime());
        }
        if (transferDto.getPhone() != null) {
            queryWrapper.lambda().like(Transfer::getPhone, transferDto.getPhone());
        }
        if (transferDto.getCustomerName() != null) {
            queryWrapper.lambda().eq(Transfer::getUserName, transferDto.getCustomerName());
        }
        IPage<Transfer> page = transferMapper.selectPage(iPage, queryWrapper);

        PageResult<Transfer> pageResult = new PageResult<>(page.getTotal(), page.getRecords());

        return Result.success(pageResult);
    }

    @Override
    public Result findById(Integer id) {

        Transfer transfer = transferMapper.selectById(id);
        return Result.success(transfer);
    }

    @Override
    public Result deptList() {

        List<SysDept> sysDepts = sysDeptMapper.selectList(null);

        return Result.success(sysDepts);
    }

    @Override
    public Result userList() {
        List<SysUser> users = sysUserMapper.selectList(null);

        return Result.success(users);
    }

    @Override
    public Result transferClubAndBusiness(TransferVo transferVo) {

        //获取userId
        String userId = transferVo.getUserId();
        QueryWrapper<Clue> clueQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Customer> businessQueryWrapper = new QueryWrapper<>();

        clueQueryWrapper.lambda().eq(Clue::getPhone,transferVo.getPhone());
        businessQueryWrapper.lambda().eq(Customer::getPhone,transferVo.getPhone());

        List<Clue> clues = clueMapper.selectList(clueQueryWrapper);
        clues.forEach(clue -> clue.setBelonger(transferVo.getUserName()));
        clues.forEach(clue -> clueMapper.insert(clue));

        List<Customer> customers = businessMapper.selectList(businessQueryWrapper);
        customers.forEach(customer -> customer.setBelonger(transferVo.getUserName()));
        customers.stream().forEach(customer -> businessMapper.insert(customer));

        return Result.success();
    }
}
